﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using ELMApplication.Common.Model;

using ELMApplication.Common.Const;
using System.Linq;
using Microsoft.Practices.Composite.Events;
using ELMApplication.Common.Events;
using UserLoginModule.Controllers;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Modularity;
using ELMWCFServiceClass.ELMWCF;

namespace UserLoginModule.Views.UserLoginView
{
    public class UserLoginPrentationModel : IUserLoginPresentationModel
    {
        private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        //private readonly ILoginCommand _loginCommand;
        public UserLoginPrentationModel(IUserLoginView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManager, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;
            
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.moduleManager = moduleManager;
            this.View = view;
           
           // _eventAggregator.GetEvent<DataSubmitedEvent>().Subscribe(msg, true);
            view.SetModel(this);
           
        }
       
      
       
       

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region IUserLoginPresentationModel Members

        public IUserLoginView View
        {
            get;
            set;
        }


        public void Login()
        {
            
           
            _domaincontext.GetT_HummanByAccountAsync(UName, UPassword);
            _domaincontext.GetT_HummanByAccountCompleted += (sender,e) => {
                if (e.Error == null&&e.Result!=null)
                {
                                   
                    Current.User = e.Result;
                  
                        moduleManager.LoadModule("DataflowListModule");
                        IRegion region = this.regionManager.Regions["MainRegion"];

                        // region.Remove(View);
                        // regionManager.Regions[RegionNames.DataListRegion.ToString()].Activate(regionManager.Regions[RegionNames.DataListRegion.ToString()].GetView("DataflowListView"));
                        NotifyPropertyChanged("ISOK");
                        _eventAggregator.GetEvent<TempEvent>().Publish(new Dataflow());
                    
                }
                else
                {
                    MessageBox.Show("帐号密码错误！");
                }
            
            };
           
           

        }

     



        private string _uname;
        private string _password;
        public string UName
        {
            get
            {
                
                return _uname;
            }
            set
            {
                
                _uname = value;
                
                NotifyPropertyChanged("UName");
            }
        }

        public string UPassword
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
                NotifyPropertyChanged("UPassword");
            }
        }

        #endregion



      
    }
}
